﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KVB.Common;
using System.Text;
using Customer_Profile.Admin;

namespace Customer_Profile.ClientManage
{
    public partial class CustomerList : System.Web.UI.Page
    {
        CustomerDao da = new CustomerDao();
        string operatorname = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["usermodel"] != null)
            {
                operatorname = ((UserInfoEntity)Session["usermodel"]).UserName;
                if (!Page.IsPostBack)
                {
                    Comm.BindDll(ref this.ddlAdvisor, "Advisor", true, "");
                    Comm.BindDll(ref this.ddllocal, "Local", true, "");
                    Comm.BindDll(ref this.ddlstatus, "status", true, "");
                    GetGvData();
                }
            }
            else
            {
                Response.Redirect("/login.aspx", true);
            }
        }

        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            GetGvData();
        }

        private void GetGvData()
        {
            try
            {
                string where = this.GetWhere();
                DataTable dt = new DataTable();
                dt = da.GetListByCondition(where);
                if (dt != null)
                {
                    GvData.DataSource = dt;
                    GvData.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("GetGvData", ex.ToString());
            }
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton Delbtn = (LinkButton)e.Row.Cells[6].Controls[1];
                Delbtn.Attributes.Add("onclick", "javascript:return confirm('Change Status,Are You Sure?');");
                Delbtn.Attributes.Add("Guid", ((DataRowView)(e.Row.DataItem)).Row.ItemArray[0].ToString());
            }
        }

        private string GetWhere()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" where ");
                if (this.txtFirstName.Value != "")
                {
                    sb.Append(" FirstName like '%" + this.replace(this.txtFirstName.Value) + "%' and ");
                }
                if (this.txtLastName.Value != "")
                {
                    sb.Append(" LastName like '%" + this.replace(this.txtLastName.Value) + "%' and ");
                }
                if (this.txthometel.Value != "")
                {
                    sb.Append(" HomeNo like '%" + this.replace(this.txthometel.Value) + "%' and ");
                }
                if (this.txtworktel.Value != "")
                {
                    sb.Append(" WorkNo like '%" + this.replace(this.txtworktel.Value) + "%' and ");
                }
                if (this.txtFax.Value != "")
                {
                    sb.Append(" FaxNo like '%" + this.replace(this.txtFax.Value) + "%' and ");
                }
                if (this.txtEmail.Value != "")
                {
                    sb.Append(" Email like '%" + this.replace(this.txtEmail.Value) + "%' and ");
                }
                //if (this.txtacno.Value != "")
                //{
                //    sb.Append(" ACNo like '%" + this.replace(this.txtacno.Value) + "%' and ");
                //}
                if (this.txtibid.Value != "")
                {
                    sb.Append(" IBID like '%" + this.replace(this.txtibid.Value) + "%' and ");
                }
                if (this.txtibloginname.Value != "")
                {
                    sb.Append(" IBLoginName like '%" + this.replace(this.txtibloginname.Value) + "%' and ");
                }
                if (this.txtkvbid.Value != "")
                {
                    sb.Append(" KVBID like '%" + this.replace(this.txtkvbid.Value) + "%' and ");
                }
                if (this.txtfinno.Value != "")
                {
                    sb.Append(" FINNo like '%" + this.replace(this.txtfinno.Value) + "%' and ");
                }
                if (this.txtcsnno.Value != "")
                {
                    sb.Append(" CSNNo like '%" + this.replace(this.txtcsnno.Value) + "%' and ");
                }
                //if (this.txtaddress.Value != "")
                //{
                //    sb.Append(" Address like '%" + this.replace(this.txtaddress.Value) + "%' and ");
                //}
                //if (this.txtcity.Value != "")
                //{
                //    sb.Append(" City like '%" + this.replace(this.txtcity.Value) + "%' and ");
                //}
                //if (this.txtProvince.Value != "")
                //{
                //    sb.Append(" Province like '%" + this.replace(this.txtProvince.Value) + "%' and ");
                //}
                //if (this.txtremarks.Text != "")
                //{
                //    sb.Append(" Remarks like '%" + this.replace(this.txtremarks.Text) + "%' and ");
                //}
                //if (this.txtcountry.Value != "")
                //{
                //    sb.Append(" Country like '%" + this.replace(this.txtcountry.Value) + "%' and ");
                //}
                //if (this.txtPostcode.Value != "")
                //{
                //    sb.Append(" PostalCode like '%" + this.replace(this.txtPostcode.Value) + "%' and ");
                //}
                //if (this.ddltitle.SelectedIndex != 0)
                //{
                //    sb.Append(" Title ='" + this.replace(this.ddltitle.SelectedValue) + "' and ");
                //}
                if (this.ddlAdvisor.SelectedIndex != 0)
                {
                    sb.Append(" Advisor ='" + this.replace(this.ddlAdvisor.SelectedValue) + "' and ");
                }
                if (this.ddllocal.SelectedIndex != 0)
                {
                    sb.Append(" Location ='" + this.replace(this.ddllocal.SelectedValue) + "' and ");
                }
                if (this.ddlstatus.SelectedIndex != 0)
                {
                    sb.Append(" status ='" + this.replace(this.ddlstatus.SelectedValue) + "' and ");
                }
                if (this.txtcreatedatebegin.Text != "")
                {
                    sb.Append(" CreateDate >='" + this.replace(this.txtcreatedatebegin.Text) + "' and ");
                }
                if (this.txtcreatedateend.Text != "")
                {
                    sb.Append(" CreateDate <='" + this.replace(this.txtcreatedateend.Text) + " 23:59:59" + "' and ");
                }
                if (this.txtmodifydatebegin.Text != "")
                {
                    sb.Append(" ModifyDate >='" + this.replace(this.txtmodifydatebegin.Text) + "' and ");
                }
                if (this.txtmodifydateend.Text != "")
                {
                    sb.Append(" ModifyDate <='" + this.replace(this.txtmodifydateend.Text) + " 23:59:59" + "' and ");
                }
                sb.Append(" 1=1 order by createdate desc");
                return sb.ToString();
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("GetWhere", ex.ToString());
                return "";
            }
        }

        private string replace(string str)
        {
            return str.Replace("'", "''");
        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GvData.PageIndex = e.NewPageIndex;
            GetGvData();
        }

        protected void btnadd_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("CustomerAdd.aspx", true);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            try
            {
                string Guid = ((LinkButton)sender).Attributes["Guid"].ToString();
                da.ChangeStatus(Guid, operatorname);
                GetGvData();
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("LinkButton1_Click", ex.ToString());
            }
        }
    }
}
